package com.phone.servlet;

import com.phone.entity.Result;
import com.phone.entity.Results;
import com.phone.exception.ParameterException;
import com.phone.utils.TextUtil;
import org.hibernate.Session;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.annotation.XmlType;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by tisen on 2017/7/5.
 */
public class BaseServlet extends HttpServlet {
    public static final String ERROR = "error";
    public static final String SQLERROR = "sql error";
    public static final String SUCCESS = "successful";
    private HttpServletRequest request = null;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        out(response, "hello world");
    }

    public void out(HttpServletResponse response, String tip) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println(tip);
        out.flush();
        out.close();
    }

    public void out(HttpServletResponse response, Result result) throws IOException {
        out(response, new JSONObject(result).toString());
    }

    public void out(HttpServletResponse response, Results results) throws IOException {
        out(response, new JSONObject(results).toString());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    public void setParamterRequest(HttpServletRequest request) {
        this.request = request;
    }

    public String getParamterString(String key, boolean nullable) throws Exception {
        String value = request.getParameter(key);
        if (!nullable && TextUtil.textIsEmpty(value))
            throw new ParameterException(ParameterException.PARAMETER_IS_EMPTY, key);
        return value;
    }

    public String getParamterString(String key, boolean nullable, String defaultString) throws Exception {
        String value = request.getParameter(key);
        if (!nullable && TextUtil.textIsEmpty(value))
            throw new ParameterException(ParameterException.PARAMETER_IS_EMPTY, key);
        if (TextUtil.textIsEmpty(value))
            return defaultString;
        else
            return value;
    }

    public int getParamterInt(String key, boolean nullable, int defValue) throws Exception {
        String value = getParamterString(key, nullable);
        if (TextUtil.textIsEmpty(value)) {
            return defValue;
        } else {
            int i = Integer.parseInt(value);
            return i;
        }
    }

    public float getParamterFloat(String key, boolean nullable, float defValue) throws Exception {
        String value = getParamterString(key, nullable);
        if (TextUtil.textIsEmpty(value)) {
            return defValue;
        } else {
            float f = Float.parseFloat(value);
            return f;
        }
    }

    public boolean getParamterBoolean(String key, boolean nullable, boolean defValue) throws Exception {
        String value = getParamterString(key, nullable);
        if (TextUtil.textIsEmpty(value)) {
            return defValue;
        } else {
            boolean f = Boolean.parseBoolean(value);
            return f;
        }
    }
}
